{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import igraph as ig\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.animation as animation\n",
    "\n",
    "# 创建环形图\n",
    "g = ig.Graph.Ring(10, directed=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 环状布局\n",
    "layout = g.layout_circle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这个回调汉书会在每一帧运行，并将帧号作为参数；每一帧只计算一小部分顶点和边的子图。随着时间推移, 图边的越来越完整直到整个环闭合\n",
    "def _update_graph(frame):\n",
    "    # Remove plot elements from the previous frame\n",
    "    ax.clear()\n",
    "\n",
    "    # Fix limits (unless you want a zoom-out effect)\n",
    "    ax.set_xlim(-1.5, 1.5)\n",
    "    ax.set_ylim(-1.5, 1.5)\n",
    "\n",
    "    if frame < 10:\n",
    "        # Plot subgraph\n",
    "        gd = g.subgraph(range(frame))\n",
    "    elif frame == 10:\n",
    "        # In the second-to-last frame, plot all vertices but skip the last\n",
    "        # edge, which will only be shown in the last frame\n",
    "        gd = g.copy()\n",
    "        gd.delete_edges(9)\n",
    "    else:\n",
    "        # Last frame\n",
    "        gd = g\n",
    "\n",
    "    ig.plot(gd, target=ax, layout=layout[:frame], vertex_color=\"yellow\")\n",
    "\n",
    "    # Capture handles for blitting\n",
    "    if frame == 0:\n",
    "        nhandles = 0\n",
    "    elif frame == 1:\n",
    "        nhandles = 1\n",
    "    elif frame < 11:\n",
    "        # vertex, 2 for each edge\n",
    "        nhandles = 3 * frame\n",
    "    else:\n",
    "        # The final edge closing the circle\n",
    "        nhandles = 3 * (frame - 1) + 2\n",
    "\n",
    "    handles = ax.get_children()[:nhandles]\n",
    "    return handles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "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ADEFAGIKAMQUAIgpABBTACCmAEBMAYCYAgAxBQBiCgDEFACIKQAQUwAgpgBATAGAmAIAMQUAYgoAxBQAiCkAEFMAIKYAQEwBgJgCADEFAGIKAMQUAIgpABBTACCmAEBMAYCYAgAxBQBiCgDEFACIKQAQUwAgpgBATAGAmAIAMQUAYgoAxBQAiCkAEFMAIKYAQEwBgJgCADEFAGIKAMQUAIgpABBTACCmAEBMAYCYAgAxBQBiCgDEFACIKQAQUwAgpgBATAGAmAIAMQUAYgoAxBQAiCkAEFMAIKYAQEwBgJgCADEFAGIKAMQUAIgpABBTACCmAEBMAYCYAgAxBQBiCgDEFACIKQAQUwAgpgBATAGAmAIAMQUAYgoAxBQAiCkAEFMAIKYAQEwBgJgCADEFAGIKAMQUAIgpABBTACCmAEBMAYCYAgAxBQBiCgDEFACIKQAQUwAgpgBATAGAmAIAMQUAYgoAxBQAiCkAEFMAIKYAQEwBgJgCADEFAGIKAMQUAIgpABBTACCmAEBMAYCYAgAxBQBiCgDEFACIKQAQUwAgpgBATAGAmAIAMQUAYgoAxBQAiCkAEFMAIKYAQEwBgJgCADEFAGIKAMQUAIgpABBTACCmAEBMAYCYAgAxBQBiCgDEFACIKQAQUwAgpgBATAGAmAIAMQUAYgoAxBQAiCkAEFMAIKYAQEwBgJgCADEFAGIKAMQUAIgpABBTACCmAEBMAYCYAgAxBQBiCgDEFACIKQAQUwAgpgBAfu8DBwYENNNsAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ani = animation.FuncAnimation(fig, _update_graph, 12, interval=500, blit=True)\n",
    "plt.ion()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 ('py310')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "49ec5068d4d6d3736c39d3918a16f1ca551a23384109b69898c01c2015d1370c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
